<template><div><h2 id="表单验证" tabindex="-1"><a class="header-anchor" href="#表单验证"><span>表单验证</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#validation-quickstart">快速开始</a>
<ul>
<li><a href="#quick-defining-the-routes">定义路由</a></li>
<li><a href="#quick-creating-the-controller">创建控制器</a></li>
<li><a href="#quick-writing-the-validation-logic">编写验证逻辑</a></li>
<li><a href="#quick-displaying-the-validation-errors">显示验证错误信息</a></li>
<li><a href="#repopulating-forms">回填表单</a></li>
<li><a href="#a-note-on-optional-fields">可选字段的注意事项</a></li>
<li><a href="#validation-error-response-format">验证错误响应的格式化</a></li>
</ul>
</li>
<li><a href="#form-request-validation">表单请求验证</a>
<ul>
<li><a href="#creating-form-requests">创建表单请求类</a></li>
<li><a href="#authorizing-form-requests">表单请求授权验证</a></li>
<li><a href="#customizing-the-error-messages">自定义错误消息</a></li>
<li><a href="#preparing-input-for-validation">表单输入预处理</a></li>
</ul>
</li>
<li><a href="#manually-creating-validators">手动创建验证器</a>
<ul>
<li><a href="#automatic-redirection">自动重定向</a></li>
<li><a href="#named-error-bags">命名错误包</a></li>
<li><a href="#manual-customizing-the-error-messages">自定义错误消息</a></li>
<li><a href="#after-validation-hook">验证后的钩子</a></li>
</ul>
</li>
<li><a href="#working-with-validated-input">使用验证后的表单输入</a></li>
<li><a href="#working-with-error-messages">使用验证错误信息</a>
<ul>
<li><a href="#specifying-custom-messages-in-language-files">在本地化文件中指定自定义消息</a></li>
<li><a href="#specifying-attribute-in-language-files">在本地化文件中指定属性</a></li>
<li><a href="#specifying-values-in-language-files">在本地化文件中指定值</a></li>
</ul>
</li>
<li><a href="#available-validation-rules">可用的验证规则</a></li>
<li><a href="#conditionally-adding-rules">按条件添加验证规则</a></li>
<li><a href="#validating-arrays">验证数组</a>
<ul>
<li><a href="#validating-nested-array-input">验证多维数组</a></li>
<li><a href="#error-message-indexes-and-positions">错误消息的索引和定位</a></li>
</ul>
</li>
<li><a href="#validating-files">验证文件</a></li>
<li><a href="#validating-passwords">验证密码</a></li>
<li><a href="#custom-validation-rules">自定义验证规则</a>
<ul>
<li><a href="#using-rule-objects">使用 Rule 对象</a></li>
<li><a href="#using-closures">使用闭包函数</a></li>
<li><a href="#implicit-rules">隐式规则</a></li>
</ul>
</li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Laravel 提供了几种不同的方法来验证传入应用程序的数据。最常见的做法是在所有传入的 HTTP 请求中使用 <code v-pre>validate</code> 方法。同时，我们还将讨论其他验证方法。</p>
<p>Laravel 包含了各种方便的验证规则，你可以将它们应用于数据，甚至可以验证给定数据库表中的值是否唯一。我们将详细介绍每个验证规则，以便你熟悉 Laravel 的所有验证功能。</p>
<h2 id="快速开始" tabindex="-1"><a class="header-anchor" href="#快速开始"><span>快速开始</span></a></h2>
<p>为了了解 Laravel 强大的验证功能，我们来看一个表单验证并将错误消息展示给用户的完整示例。通过阅读概述，这将会对你如何使用 Laravel 验证传入的请求数据有一个很好的理解：</p>
<h3 id="定义路由" tabindex="-1"><a class="header-anchor" href="#定义路由"><span>定义路由</span></a></h3>
<p>首先，假设我们在 <code v-pre>routes/web.php</code> 路由文件中定义了下面这些路由：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>PostController</span><span class="token punctuation">;</span> </span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/post/create'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token class-name static-context">PostController</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'create'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/post'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token class-name static-context">PostController</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'store'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>GET</code> 路由会显示一个供用户创建新博客文章的表单，而 <code v-pre>POST</code> 路由会将新的博客文章存储到数据库中。</p>
<h3 id="创建控制器" tabindex="-1"><a class="header-anchor" href="#创建控制器"><span>创建控制器</span></a></h3>
<p>接下来，让我们一起来看看处理这些路由的简单控制器。我们暂时留空了 store 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>RedirectResponse</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>View<span class="token punctuation">\</span>View</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">PostController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 博客的表单视图</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">create</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">View</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post.create'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 存储博客的 Action</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">store</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">RedirectResponse</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// 验证并且执行存储逻辑</span></span>
<span class="line"></span>
<span class="line">        <span class="token variable">$post</span> <span class="token operator">=</span> <span class="token doc-comment comment">/** ... */</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">to_route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post.show'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'post'</span> <span class="token operator">=></span> <span class="token variable">$post</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="编写验证逻辑" tabindex="-1"><a class="header-anchor" href="#编写验证逻辑"><span>编写验证逻辑</span></a></h3>
<p>现在我们开始在 <code v-pre>store</code> 方法中编写用来验证新的博客文章的逻辑代码。为此，我们将使用 <code v-pre>Illuminate\Http\Request</code> 类提供的 <code v-pre>validate</code> 方法。如果验证通过，你的代码会继续正常运行。如果验证失败，则会抛出 <code v-pre>Illuminate\Validation\ValidationException</code> 异常，并自动将对应的错误响应返回给用户。</p>
<p>如果在传统 HTTP 请求期间验证失败，则会生成对先前 URL 的重定向响应。如果传入的请求是 XHR，将将返回包含验证错误信息的 JSON 响应。</p>
<p>为了深入理解 <code v-pre>validate</code> 方法，让我们接着回到 <code v-pre>store</code> 方法中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 存储一篇新的博客文章。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">store</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">RedirectResponse</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 博客文章验证通过...</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/posts'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如你所见，验证规则被传递到 <code v-pre>validate</code> 方法中。不用担心——所有可用的验证规则均已 <a href="#available-validation-rules">存档</a>。 另外再提醒一次，如果验证失败，会自动生成一个对应的响应。如果验证通过，那我们的控制器会继续正常运行。</p>
<p>另外，验证规则可以使用数组，而不是单个 <code v-pre>|</code> 分隔的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validatedData</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'unique:posts'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'max:255'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'required'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此外，你可以使用 <code v-pre>validateWithBag</code> 方法来验证请求，并将所有错误信息储存在一个 <a href="#named-error-bags">命名错误信息包</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validatedData</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validateWithBag</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'unique:posts'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'max:255'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'required'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="在首次验证失败时停止运行" tabindex="-1"><a class="header-anchor" href="#在首次验证失败时停止运行"><span>在首次验证失败时停止运行</span></a></h4>
<p>有时候我们希望某个字段在第一次验证失败后就停止运行验证规则，只需要将 <code v-pre>bail</code> 添加到规则中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'bail|required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在这个例子中，如果 <code v-pre>title</code> 字段没有通过 <code v-pre>unique</code> 规则，那么不会继续验证 <code v-pre>max</code> 规则。规则会按照分配时的顺序来验证。</p>
<h4 id="嵌套字段的说明" tabindex="-1"><a class="header-anchor" href="#嵌套字段的说明"><span>嵌套字段的说明</span></a></h4>
<p>如果传入的 HTTP 请求包含「嵌套」参数，你可以在验证规则中使用<code v-pre>.</code>语法来指定这些参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'author.name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'author.description'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>另外，如果你的字段名称包含点，则可以通过使用反斜杠将点转义，以防止将其解释为<code v-pre>.</code>语法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'v1\.0'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="显示验证错误信息" tabindex="-1"><a class="header-anchor" href="#显示验证错误信息"><span>显示验证错误信息</span></a></h3>
<p>那么，如果传入的请求字段没有通过验证规则呢？如前所述，Laravel 会自动将用户重定向到之前的位置。此外，所有的验证错误和<a href="https://learnku.com/docs/laravel/10.x/requests#retrieving-old-input" target="_blank" rel="noopener noreferrer">请求输入</a>都会自动存入到<a href="https://learnku.com/docs/laravel/10.x/session#flash-data" target="_blank" rel="noopener noreferrer">闪存 session</a> 中。</p>
<p><code v-pre>Illuminate\View\Middleware\ShareErrorsFromSession</code>中间件与应用程序的所有视图共享一个<code v-pre>$errors</code>变量，该变量由<code v-pre>web</code>中间件组提供。当应用该中间件时，<code v-pre>$errors</code> 变量始终在视图中可用，<code v-pre>$errors</code> 变量是 <code v-pre>Illuminate\Support\MessageBag</code> 的实例。更多有关使用该对象的信息，<a href="#working-with-error-messages">查看文档</a></p>
<p>因此，在实例中，当验证失败时，用户将重定向到控制器<code v-pre>create</code>方法，从而在视图中显示错误消息：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;!-- /resources/views/post/create.blade.php --&gt;</span>
<span class="line"></span>
<span class="line">&lt;h1&gt;Create Post&lt;/h1&gt;</span>
<span class="line"></span>
<span class="line">@if ($errors-&gt;any())</span>
<span class="line">    &lt;div class=&quot;alert alert-danger&quot;&gt;</span>
<span class="line">        &lt;ul&gt;</span>
<span class="line">            @foreach ($errors-&gt;all() as $error)</span>
<span class="line">                &lt;li&gt;{{ $error }}&lt;/li&gt;</span>
<span class="line">            @endforeach</span>
<span class="line">        &lt;/ul&gt;</span>
<span class="line">    &lt;/div&gt;</span>
<span class="line">@endif</span>
<span class="line"></span>
<span class="line">&lt;!-- Create Post Form --&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="在语言文件中指定自定义消息" tabindex="-1"><a class="header-anchor" href="#在语言文件中指定自定义消息"><span>在语言文件中指定自定义消息</span></a></h4>
<p>Laravel 的内置验证规则每个都对应一个错误消息，位于应用程序的<code v-pre>lang/en/validation.php</code>文件中。在此文件中，你将找到每个验证规则的翻译条目。你可以根据应用程序的需求随意更改或修改这些消息。</p>
<p>此外，你可以将此文件复制到另一个翻译语言目录中，以翻译应用程序语言的消息。要了解有关 Laravel 本地化的更多信息，请查看完整的<a href="https://learnku.com/docs/laravel/10.x/localization" target="_blank" rel="noopener noreferrer">本地化文档</a>.</p>
<blockquote>
<p><strong>注意</strong><br>
默认，Laravel 应用程序框架不包括<code v-pre>lang</code>目录。如果你想自定义 Laravel 的语言文件，你可以通过<code v-pre>lang:publish</code> Artisan 命令发布它们。</p>
</blockquote>
<h4 id="xhr-请求-验证" tabindex="-1"><a class="header-anchor" href="#xhr-请求-验证"><span>XHR 请求 &amp; 验证</span></a></h4>
<p>在如下示例中，我们使用传统形式将数据发送到应用程序。但是，许多应用程序从 JavaScript 驱动的前端接收 XHR 请求。在 XHR 请求期间使用<code v-pre>validate</code>方法时，Laravel 将不会生成重定向响应。相反，Laravel生成一个<a href="#validation-error-response-format">包含所有验证错误的 JSON 响应</a>。该 JSON 响应将以 422 HTTP 状态码发送。</p>
<h4 id="error指令" tabindex="-1"><a class="header-anchor" href="#error指令"><span><code v-pre>@error</code>指令</span></a></h4>
<p>你亦可使用 <code v-pre>@error</code> <a href="https://learnku.com/docs/laravel/10.x/blade" target="_blank" rel="noopener noreferrer">Blade</a> 指令方便地检查给定的属性是否存在验证错误信息。在<code v-pre>@error</code>指令中，你可以输出<code v-pre>$message</code>变量以显示错误信息：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;!-- /resources/views/post/create.blade.php --&gt;</span>
<span class="line"></span>
<span class="line">&lt;label for=&quot;title&quot;&gt;Post Title&lt;/label&gt;</span>
<span class="line"></span>
<span class="line">&lt;input id=&quot;title&quot;</span>
<span class="line">    type=&quot;text&quot;</span>
<span class="line">    name=&quot;title&quot;</span>
<span class="line">    class=&quot;@error('title') is-invalid @enderror&quot;&gt;</span>
<span class="line"></span>
<span class="line">@error('title')</span>
<span class="line">    &lt;div class=&quot;alert alert-danger&quot;&gt;{{ $message }}&lt;/div&gt;</span>
<span class="line">@enderror</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你使用<a href="#named-error-bags">命名错误包</a>，你可以将错误包的名称作为第二个参数传递给<code v-pre>@error</code>指令：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;input ... class=&quot;@error('title', 'post') is-invalid @enderror&quot;&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="回填表单" tabindex="-1"><a class="header-anchor" href="#回填表单"><span>回填表单</span></a></h3>
<p>当 Laravel 由于验证错误而生成重定向响应时，框架将自动<a href="https://learnku.com/docs/laravel/10.x/session#flash-data" target="_blank" rel="noopener noreferrer">将所有请求的输入闪存到 session 中</a>。这样做是为了方便你在下一个请求期间访问输入，并重新填充用户尝试提交的表单。</p>
<p>要从先前的请求中检索闪存的输入，请在 <code v-pre>Illuminate\Http\Request</code>的实例上调用<code v-pre>old</code>方法。 <code v-pre>old</code>方法将从 <a href="https://learnku.com/docs/laravel/10.x/session" target="_blank" rel="noopener noreferrer">session</a> 中提取先前闪存的输入数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$title</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">old</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'title'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Laravel 还提供了一个全局性的<code v-pre>old</code>。如果要在 <a href="https://learnku.com/docs/laravel/10.x/blade" target="_blank" rel="noopener noreferrer">Blade 模板</a>, 中显示旧输入，则使用<code v-pre>old</code>来重新填充表单会更加方便。如果给定字段不存在旧输入，则将返回<code v-pre>null</code>：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;input type=&quot;text&quot; name=&quot;title&quot; value=&quot;{{ old('title') }}&quot;&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="关于可选字段的注意事项" tabindex="-1"><a class="header-anchor" href="#关于可选字段的注意事项"><span>关于可选字段的注意事项</span></a></h3>
<p>默认情况下， 在你的 Laravel 应用的全局中间件堆栈<code v-pre>App\Http\Kernel</code>类中包含了<code v-pre>TrimStrings</code>和<code v-pre>ConvertEmptyStringsToNull</code>中间件。因此，如果你不想让<code v-pre>null</code>被验证器标识为非法的话，你需要将「可选」字段标志为<code v-pre>nullable</code>。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'publish_at'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'nullable|date'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在此示例中，我们指定 <code v-pre>publish_at</code> 字段可以为 <code v-pre>null</code> 或有效的日期表示。如果没有将 <code v-pre>nullable</code> 修饰符添加到规则定义中，则验证器会将 <code v-pre>null</code> 视为无效日期。</p>
<h3 id="验证错误响应格式" tabindex="-1"><a class="header-anchor" href="#验证错误响应格式"><span>验证错误响应格式</span></a></h3>
<p>当您的应用程序抛出 <code v-pre>Illuminate\Validation\ValidationException</code> 异常，并且传入的 HTTP 请求希望返回 JSON 响应时，Laravel 将自动为您格式化错误消息，并返回 <code v-pre>422 Unprocessable Entity</code> HTTP 响应。</p>
<p>下面是验证错误的 JSON 响应格式示例。请注意，嵌套的错误键会被转换为“点”符号格式：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"The team name must be a string. (and 4 more errors)"</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token property">"errors"</span><span class="token operator">:</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token property">"team_name"</span><span class="token operator">:</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string">"The team name must be a string."</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string">"The team name must be at least 1 characters."</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"authorization.role"</span><span class="token operator">:</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string">"The selected authorization.role is invalid."</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"users.0.email"</span><span class="token operator">:</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string">"The users.0.email field is required."</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"users.2.email"</span><span class="token operator">:</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string">"The users.2.email must be a valid email address."</span></span>
<span class="line">        <span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="表单请求验证" tabindex="-1"><a class="header-anchor" href="#表单请求验证"><span>表单请求验证</span></a></h2>
<h3 id="创建表单请求" tabindex="-1"><a class="header-anchor" href="#创建表单请求"><span>创建表单请求</span></a></h3>
<p>对于更复杂的验证场景，您可能希望创建一个“表单请求”。表单请求是自定义请求类，封装了自己的验证和授权逻辑。要创建一个表单请求类，您可以使用 <code v-pre>make:request</code> Artisan CLI 命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan make:request StorePostRequest</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>生成的表单请求类将被放置在 <code v-pre>app/Http/Requests</code> 目录中。如果此目录不存在，则在运行 <code v-pre>make:request</code> 命令时将创建该目录。Laravel 生成的每个表单请求都有两个方法：<code v-pre>authorize</code> 和 <code v-pre>rules</code>。</p>
<p>你可能已经猜到了，<code v-pre>authorize</code> 方法负责确定当前已认证用户是否可以执行请求所代表的操作，而 <code v-pre>rules</code> 方法返回应用于请求数据的验证规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取应用于请求的验证规则。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, \Illuminate\Contracts\Validation\Rule|array|string></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">rules</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
你可以在 <code v-pre>rules</code> 方法的签名中指定任何你需要的依赖项类型提示。它们将通过 Laravel 的 <a href="https://learnku.com/docs/laravel/10.x/container" target="_blank" rel="noopener noreferrer">服务容器</a> 自动解析。</p>
</blockquote>
<p>那么，验证规则是如何被评估的呢？你只需要在控制器方法中对请求进行类型提示。在调用控制器方法之前，传入的表单请求将被验证，这意味着你不需要在控制器中添加任何验证逻辑：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 存储新博客文章。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">store</span><span class="token punctuation">(</span><span class="token class-name type-declaration">StorePostRequest</span> <span class="token variable">$request</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">RedirectResponse</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 传入的请求有效...</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 检索已验证的输入数据...</span></span>
<span class="line">    <span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validated</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Retrieve a portion of the validated input data...</span></span>
<span class="line">    <span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">only</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">except</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 存储博客文章...</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/posts'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果验证失败，将生成重定向响应以将用户发送回其先前的位置。错误也将被闪存到会话中，以便进行显示。如果请求是 XHR 请求，则会向用户返回带有 422 状态代码的 HTTP 响应，其中包含<a href="#validation-error-response-format">JSON 格式的验证错误表示</a>。</p>
<h4 id="在表单请求后添加钩子" tabindex="-1"><a class="header-anchor" href="#在表单请求后添加钩子"><span>在表单请求后添加钩子</span></a></h4>
<p>如果您想在表单请求「之后」添加验证钩子，可以使用 <code v-pre>withValidator</code> 方法。这个方法接收一个完整的验证构造器，允许你在验证结果返回之前调用任何方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 配置验证实例。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">withValidator</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Validator</span> <span class="token variable">$validator</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">after</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Validator</span> <span class="token variable">$validator</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">somethingElseIsInvalid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">errors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'field'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Something is wrong with this field!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="单个验证规则失败后停止" tabindex="-1"><a class="header-anchor" href="#单个验证规则失败后停止"><span>单个验证规则失败后停止</span></a></h4>
<p>通过向您的请求类添加 <code v-pre>stopOnFirstFailure</code> 属性，您可以通知验证器一旦发生单个验证失败后，停止验证所有规则。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 表示验证器是否应在第一个规则失败时停止。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">bool</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$stopOnFirstFailure</span> <span class="token operator">=</span> <span class="token constant boolean">true</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义重定向" tabindex="-1"><a class="header-anchor" href="#自定义重定向"><span>自定义重定向</span></a></h4>
<p>如前所述，当表单请求验证失败时，将会生成一个让用户返回到先前位置的重定向响应。当然，您也可以自由定义此行为。如果您要这样做，可以在表单请求中定义一个 <code v-pre>$redirect</code> 属性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 如果验证失败，用户应重定向到的 URI。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$redirect</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'/dashboard'</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，如果你想将用户重定向到一个命名路由，你可以定义一个 <code v-pre>$redirectRoute</code> 属性来代替：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 如果验证失败，用户应该重定向到的路由。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$redirectRoute</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'dashboard'</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="表单请求授权验证" tabindex="-1"><a class="header-anchor" href="#表单请求授权验证"><span>表单请求授权验证</span></a></h3>
<p>表单请求类内也包含了 <code v-pre>authorize</code> 方法。在这个方法中，您可以检查经过身份验证的用户确定其是否具有更新给定资源的权限。例如，您可以判断用户是否拥有更新文章评论的权限。最有可能的是，您将通过以下方法与你的 <a href="https://learnku.com/docs/laravel/10.x/authorization" target="_blank" rel="noopener noreferrer">授权与策略</a> 进行交互：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Comment</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 确定用户是否有请求权限。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">authorize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">bool</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$comment</span> <span class="token operator">=</span> <span class="token class-name static-context">Comment</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'comment'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$comment</span> <span class="token operator">&amp;&amp;</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">can</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'update'</span><span class="token punctuation">,</span> <span class="token variable">$comment</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>由于所有的表单请求都是继承了 Laravel 中的请求基类，所以我们可以使用 <code v-pre>user</code> 方法去获取当前认证登录的用户。同时请注意上述例子中对 <code v-pre>route</code> 方法的调用。这个方法允许你在被调用的路由上获取其定义的 URI 参数，譬如下面例子中的 <code v-pre>{comment}</code> 参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/comment/{comment}'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>因此，如果您的应用程序正在使用 <a href="https://learnku.com/docs/laravel/10.x/routing#route-model-binding" target="_blank" rel="noopener noreferrer">路由模型绑定</a>，则可以通过将解析的模型作为请求从而让您的代码更加简洁：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">can</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'update'</span><span class="token punctuation">,</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">comment</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果 <code v-pre>authorize</code> 方法返回 <code v-pre>false</code>，则会自动返回一个包含 403 状态码的 HTTP 响应，也不会运行控制器的方法。</p>
<p>如果您打算在应用程序的其它部分处理请求的授权逻辑，只需从 <code v-pre>authorize</code> 方法返回 <code v-pre>true</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 判断用户是否有请求权限。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">authorize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">bool</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token constant boolean">true</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
你可以向 <code v-pre>authorize</code> 方法传入所需的任何依赖项。它们会自动被 Laravel 提供的 <a href="https://learnku.com/docs/laravel/10.x/container" target="_blank" rel="noopener noreferrer">服务容器</a> 自动解析。</p>
</blockquote>
<h3 id="自定义错误消息" tabindex="-1"><a class="header-anchor" href="#自定义错误消息"><span>自定义错误消息</span></a></h3>
<p>你可以通过重写表单请求的 <code v-pre>messages</code> 方法来自定义错误消息。此方法应返回属性 / 规则对及其对应错误消息的数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取已定义验证规则的错误消息。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, string></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">messages</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'title.required'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'A title is required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'body.required'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'A message is required'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义验证属性" tabindex="-1"><a class="header-anchor" href="#自定义验证属性"><span>自定义验证属性</span></a></h4>
<p>Laravel 的许多内置验证规则错误消息都包含 <code v-pre>:attribute</code> 占位符。如果您希望将验证消息的 <code v-pre>:attribute</code> 部分替换为自定义属性名称，则可以重写 <code v-pre>attributes</code> 方法来指定自定义名称。此方法应返回属性 / 名称对的数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取验证错误的自定义属性</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, string></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">attributes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'email address'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="准备验证输入" tabindex="-1"><a class="header-anchor" href="#准备验证输入"><span>准备验证输入</span></a></h3>
<p>如果您需要在应用验证规则之前修改或清理请求中的任何数据，您可以使用 <code v-pre>prepareForValidation</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 准备验证数据。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">prepareForValidation</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">merge</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'slug'</span> <span class="token operator">=></span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">slug</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">slug</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>同样地，如果您需要在验证完成后对任何请求数据进行规范化，您可以使用 <code v-pre>passedValidation</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Handle a passed validation attempt.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">passedValidation</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="手动创建验证器" tabindex="-1"><a class="header-anchor" href="#手动创建验证器"><span>手动创建验证器</span></a></h2>
<p>如果您不想在请求上使用 <code v-pre>validate</code> 方法，可以使用 <code v-pre>Validator</code> <a href="https://learnku.com/laravel/10.x/facades" target="_blank" rel="noopener noreferrer">门面</a> 手动创建一个验证器实例。门面上的 <code v-pre>make</code> 方法会生成一个新的验证器实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>RedirectResponse</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">PostController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 存储新的博客文章。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">store</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">RedirectResponse</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">fails</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post/create'</span><span class="token punctuation">)</span></span>
<span class="line">                        <span class="token operator">-></span><span class="token function">withErrors</span><span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token punctuation">)</span></span>
<span class="line">                        <span class="token operator">-></span><span class="token function">withInput</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 获取验证后的输入...</span></span>
<span class="line">        <span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">validated</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 获取验证后输入的一部分...</span></span>
<span class="line">        <span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">only</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">except</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 存储博客文章...</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/posts'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>第一个参数传递给<code v-pre>make</code>方法的是要验证的数据。第二个参数是一个应用于数据的验证规则的数组。</p>
<p>在确定请求验证是否失败之后，您可以使用<code v-pre>withErrors</code>方法将错误消息闪存到会话中。使用此方法后，<code v-pre>$errors</code>变量将自动在重定向后与您的视图共享，从而可以轻松地将其显示回用户。<code v-pre>withErrors</code>方法接受验证器、<code v-pre>MessageBag</code>或PHP数组。</p>
<h4 id="单个验证规则失败后停止-1" tabindex="-1"><a class="header-anchor" href="#单个验证规则失败后停止-1"><span>单个验证规则失败后停止</span></a></h4>
<p>通过向您的请求类添加 <code v-pre>stopOnFirstFailure</code> 属性，您可以通知验证器一旦发生单个验证失败后，停止验证所有规则。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">stopOnFirstFailure</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">fails</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自动重定向" tabindex="-1"><a class="header-anchor" href="#自动重定向"><span>自动重定向</span></a></h3>
<p>如果您想手动创建验证器实例，但仍要利用HTTP请求的<code v-pre>validate</code>方法提供的自动重定向，可以在现有验证器实例上调用<code v-pre>validate</code>方法。如果验证失败，则会自动重定向用户，或者在XHR请求的情况下，将返回一个<a href="#validation-error-response-format">JSON响应</a></p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果验证失败，您可以使用<code v-pre>validateWithBag</code>方法将错误消息存储在<a href="#named-error-bags">命名错误包</a>中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">validateWithBag</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="命名的错误包" tabindex="-1"><a class="header-anchor" href="#命名的错误包"><span>命名的错误包</span></a></h3>
<p>如果您在同一页上有多个表单，您可能希望为包含验证错误的<code v-pre>MessageBag</code>命名，以便检索特定表单的错误消息。为此，将名称作为第二个参数传递给<code v-pre>withErrors</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'register'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withErrors</span><span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以通过 <code v-pre>$errors</code> 变量访问命名后的 <code v-pre>MessageBag</code> 实例：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">{{ $errors-&gt;login-&gt;first('email') }}</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="自定义错误消息-1" tabindex="-1"><a class="header-anchor" href="#自定义错误消息-1"><span>自定义错误消息</span></a></h3>
<p>如果需要，你可以提供验证程序实例使用的自定义错误消息，而不是 Laravel 提供的默认错误消息。有几种指定自定义消息的方法。首先，您可以将自定义消息作为第三个参数传递给 <code v-pre>Validator::make</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token variable">$rules</span><span class="token punctuation">,</span> <span class="token variable">$messages</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'required'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The :attribute field is required.'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在此示例中，<code v-pre>:attribute</code> 占位符将被验证中的字段的实际名称替换。您也可以在验证消息中使用其它占位符。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$messages</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'same'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The :attribute and :other must match.'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'size'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The :attribute must be exactly :size.'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'between'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The :attribute value :input is not between :min - :max.'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'in'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The :attribute must be one of the following types: :values'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="为给定属性指定自定义消息" tabindex="-1"><a class="header-anchor" href="#为给定属性指定自定义消息"><span>为给定属性指定自定义消息</span></a></h4>
<p>有时你可能希望只为特定属性指定自定义错误消息。你可以使用 <code v-pre>.</code> 表示法。首先指定属性名称，然后指定规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$messages</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email.required'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'We need to know your email address!'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="指定自定义属性值" tabindex="-1"><a class="header-anchor" href="#指定自定义属性值"><span>指定自定义属性值</span></a></h4>
<p>Laravel 的许多内置错误消息都包含一个 <code v-pre>:attribute</code> 占位符，该占位符已被验证中的字段或属性的名称替换。为了自定义用于替换特定字段的这些占位符的值，你可以将自定义属性的数组作为第四个参数传递给 <code v-pre>Validator::make</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token variable">$rules</span><span class="token punctuation">,</span> <span class="token variable">$messages</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'email address'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="验证后钩子" tabindex="-1"><a class="header-anchor" href="#验证后钩子"><span>验证后钩子</span></a></h3>
<p>验证器允许你在完成验证操作后执行附加的回调。以便你处理下一步的验证，甚至是往信息集合中添加更多的错误信息。你可以在验证器实例上使用 <code v-pre>after</code> 方法实现：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">Facades<span class="token punctuation">\</span>Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">after</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Validator</span> <span class="token variable">$validator</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">somethingElseIsInvalid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">errors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">add</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token string single-quoted-string">'field'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Something is wrong with this field!'</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">fails</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="处理验证字段" tabindex="-1"><a class="header-anchor" href="#处理验证字段"><span>处理验证字段</span></a></h2>
<p>在使用表单请求或手动创建的验证器实例验证传入请求数据后，你可能希望检索经过验证的请求数据。 这可以通过多种方式实现。 首先，你可以在表单请求或验证器实例上调用 <code v-pre>validated</code> 方法。 此方法返回已验证的数据数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validated</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">validated</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，你可以在表单请求或验证器实例上调用 <code v-pre>safe</code> 方法。 此方法返回一个 <code v-pre>Illuminate\Support\ValidatedInput</code>的实例。 该实例对象包含 <code v-pre>only</code>、<code v-pre>except</code> 和 <code v-pre>all</code> 方法来检索已验证数据的子集或整个已验证数据数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">only</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">except</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此外， <code v-pre>Illuminate\Support\ValidatedInput</code> 实例可以像数组一样被迭代和访问：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 迭代验证数据...</span></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token variable">$key</span> <span class="token operator">=></span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 访问验证数据数组...</span></span>
<span class="line"><span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$email</span> <span class="token operator">=</span> <span class="token variable">$validated</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>merge</code> 方法可以给验证过的数据添加额外的字段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">merge</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Taylor Otwell'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>collect</code> 方法以 <a href="https://learnku.com/docs/laravel/10.x/collections" target="_blank" rel="noopener noreferrer">collection</a> 实例的形式来检索验证的数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">collect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="使用错误消息" tabindex="-1"><a class="header-anchor" href="#使用错误消息"><span>使用错误消息</span></a></h2>
<p>在调用 <code v-pre>Validator</code> 实例的 <code v-pre>errors</code> 方法后，会收到一个 <code v-pre>Illuminate\Support\MessageBag</code> 实例，用于处理错误信息。自动提供给所有视图的 <code v-pre>$errors</code> 变量也是 <code v-pre>MessageBag</code> 类的一个实例。</p>
<h4 id="检索字段的第一条错误消息" tabindex="-1"><a class="header-anchor" href="#检索字段的第一条错误消息"><span>检索字段的第一条错误消息</span></a></h4>
<p><code v-pre>first</code> 方法返回给定字段的第一条错误信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$errors</span> <span class="token operator">=</span> <span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">errors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">echo</span> <span class="token variable">$errors</span><span class="token operator">-></span><span class="token function">first</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="检索一个字段的所有错误信息" tabindex="-1"><a class="header-anchor" href="#检索一个字段的所有错误信息"><span>检索一个字段的所有错误信息</span></a></h4>
<p><code v-pre>get</code> 方法用于检索一个给定字段的所有错误信息，返回值类型为数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$errors</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token variable">$message</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>对于数组表单字段，可以使用 <code v-pre>*</code> 来检索每个数组元素的所有错误信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$errors</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'attachments.*'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token variable">$message</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="检索所有字段的所有错误信息" tabindex="-1"><a class="header-anchor" href="#检索所有字段的所有错误信息"><span>检索所有字段的所有错误信息</span></a></h4>
<p><code v-pre>all</code> 方法用于检索所有字段的所有错误信息，返回值类型为数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$errors</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token variable">$message</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="判断字段是否存在错误信息" tabindex="-1"><a class="header-anchor" href="#判断字段是否存在错误信息"><span>判断字段是否存在错误信息</span></a></h4>
<p><code v-pre>has</code> 方法可用于确定一个给定字段是否存在任何错误信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$errors</span><span class="token operator">-></span><span class="token function">has</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="在语言文件中指定自定义消息-1" tabindex="-1"><a class="header-anchor" href="#在语言文件中指定自定义消息-1"><span>在语言文件中指定自定义消息</span></a></h3>
<p>Laravel 内置的验证规则都有一个错误信息，位于应用程序的 <code v-pre>lang/en/validation.php</code> 文件中。在这个文件中, 你会发现每个验证规则都有一个翻译条目。可以根据你的应用程序的需要，自由地改变或修改这些信息。</p>
<p>此外, 你可以把这个文件复制到另一个语言目录，为你的应用程序的语言翻译信息。要了解更多关于Laravel本地化的信息, 请查看完整的 <a href="https://learnku.com/docs/laravel/10.x/localization" target="_blank" rel="noopener noreferrer">本地化</a>。</p>
<blockquote>
<p><strong>Warning</strong><br>
默认情况下, Laravel 应用程序的骨架不包括 <code v-pre>lang</code> 目录. 如果你想定制 Laravel 的语言文件, 可以通过 <code v-pre>lang:publish</code> Artisan 命令发布它们。</p>
</blockquote>
<h4 id="针对特定属性的自定义信息" tabindex="-1"><a class="header-anchor" href="#针对特定属性的自定义信息"><span>针对特定属性的自定义信息</span></a></h4>
<p>可以在应用程序的验证语言文件中自定义用于指定属性和规则组合的错误信息。将自定义信息添加到应用程序的 <code v-pre>lang/xx/validation.php</code> 语言文件的 <code v-pre>custom</code> 数组中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'custom'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'We need to know your email address!'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'max'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Your email address is too long!'</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="在语言文件中指定属性" tabindex="-1"><a class="header-anchor" href="#在语言文件中指定属性"><span>在语言文件中指定属性</span></a></h3>
<p>Laravel 内置的错误信息包括一个 <code v-pre>:attribute</code> 占位符，它被替换为验证中的字段或属性的名称。如果你希望你的验证信息中的 <code v-pre>:attribute</code> 部分被替换成一个自定义的值, 可以在 <code v-pre>lang/xx/validation.php</code> 文件的 <code v-pre>attributes</code> 数组中指定自定义属性名称:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'attributes'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'email address'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>Warning</strong><br>
默认情况下, Laravel 应用程序的骨架不包括 <code v-pre>lang</code> 目录. 如果你想定制 Laravel 的语言文件, 可以通过 <code v-pre>lang:publish</code> Artisan 命令发布它们。</p>
</blockquote>
<h3 id="指定语言文件中的值" tabindex="-1"><a class="header-anchor" href="#指定语言文件中的值"><span>指定语言文件中的值</span></a></h3>
<p>Laravel 内置的验证规则错误信息包含一个 <code v-pre>:value</code> 占位符，它被替换成请求属性的当前值。然而, 你可能偶尔需要在验证信息的 <code v-pre>:value</code> 部分替换成自定义的值。 例如，如果 <code v-pre>payment_type</code> 的值为 <code v-pre>cc</code> 则需要验证信用卡号码:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'credit_card_number'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required_if:payment_type,cc'</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果这个验证规则失败了，它将产生以下错误信息:</p>
<div class="language-none line-numbers-mode" data-highlighter="prismjs" data-ext="none" data-title="none"><pre v-pre class="language-none"><code><span class="line">The credit card number field is required when payment type is cc.</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以在 <code v-pre>lang/xx/validation.php</code> 语言文件中通过定义一个 <code v-pre>values</code> 数组来指定一个更友好的提示，而不是显示 <code v-pre>cc</code> 作为支付类型值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'values'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'payment_type'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'cc'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'credit card'</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>Warning</strong><br>
默认情况下, Laravel 应用程序的骨架不包括 <code v-pre>lang</code> 目录. 如果你想定制 Laravel 的语言文件, 你可以通过 <code v-pre>lang:publish</code> Artisan 命令发布它们。</p>
</blockquote>
<p>定义这个值后，验证规则将产生以下错误信息：</p>
<div class="language-none line-numbers-mode" data-highlighter="prismjs" data-ext="none" data-title="none"><pre v-pre class="language-none"><code><span class="line">The credit card number field is required when payment type is credit card.</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="可用的验证规则" tabindex="-1"><a class="header-anchor" href="#可用的验证规则"><span>可用的验证规则</span></a></h2>
<p>下面是所有可用的验证规则及其功能的列表:</p>
<h4 id="accepted" tabindex="-1"><a class="header-anchor" href="#accepted"><span>accepted</span></a></h4>
<p>待验证字段必须是 <code v-pre>「yes」</code> ，<code v-pre>「on」</code> ，<code v-pre>1</code> 或 <code v-pre>true</code>。这对于验证「服务条款」的接受或类似字段时很有用。</p>
<h4 id="accepted-if-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#accepted-if-anotherfield-value"><span>accepted_if:anotherfield,value,…</span></a></h4>
<p>如果另一个正在验证的字段等于指定的值，则验证中的字段必须为 <code v-pre>「yes」</code> ，<code v-pre>「on」</code> ，<code v-pre>1</code> 或 <code v-pre>true</code>。 这对于验证「服务条款」接受或类似字段很有用。</p>
<h4 id="active-url" tabindex="-1"><a class="header-anchor" href="#active-url"><span>active_url</span></a></h4>
<p>根据 <code v-pre>dns_get_record</code> PHP 函数，验证中的字段必须具有有效的 A 或 AAAA 记录。 提供的 URL 的主机名使用 <code v-pre>parse_url</code> PHP 函数提取，然后传递给 <code v-pre>dns_get_record</code>。</p>
<h4 id="after-date" tabindex="-1"><a class="header-anchor" href="#after-date"><span>after:<em>date</em></span></a></h4>
<p>验证中的字段必须是给定日期之后的值。日期将被传递给 <code v-pre>strtotime</code> PHP 函数中，以便转换为有效的 <code v-pre>DateTime</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'start_date'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|date|after:tomorrow'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你也可以指定另一个要与日期比较的字段，而不是传递要由 <code v-pre>strtotime</code> 处理的日期字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'finish_date'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|date|after:start_date'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="after-or-equal-date" tabindex="-1"><a class="header-anchor" href="#after-or-equal-date"><span>after_or_equal:<em>date</em></span></a></h4>
<p>待验证字段的值对应的日期必须在给定日期之后或与给定的日期相同。可参阅 <a href="#rule-after">after</a> 规则获取更多信息。</p>
<h4 id="alpha" tabindex="-1"><a class="header-anchor" href="#alpha"><span>alpha</span></a></h4>
<p>待验证字段必须是包含在 <a href="https://util.unicode.org/UnicodeJsps/list-unicodeset.jsp?a=%5B%3AL%3A%5D&amp;g=&amp;i=" target="_blank" rel="noopener noreferrer"><code v-pre>\p{L}</code></a> 和 <a href="https://util.unicode.org/UnicodeJsps/list-unicodeset.jsp?a=%5B%3AM%3A%5D&amp;g=&amp;i=" target="_blank" rel="noopener noreferrer"><code v-pre>\p{M}</code></a> 中的Unicode字母字符。</p>
<p>为了将此验证规则限制在 ASCII 范围内的字符（<code v-pre>a-z</code> 和<code v-pre>A-Z</code>），你可以为验证规则提供 <code v-pre>ascii</code> 选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'alpha:ascii'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="alpha-dash" tabindex="-1"><a class="header-anchor" href="#alpha-dash"><span>alpha_dash</span></a></h4>
<p>被验证的字段必须完全是 Unicode 字母数字字符中的 <a href="https://util.unicode.org/UnicodeJsps/list-unicodeset.jsp?a=%5B%3AL%3A%5D&amp;g=&amp;i=" target="_blank" rel="noopener noreferrer"><code v-pre>\p{L}</code></a>、<a href="https://util.unicode.org/UnicodeJsps/list-unicodeset.jsp?a=%5B%3AM%3A%5D&amp;g=&amp;i=" target="_blank" rel="noopener noreferrer"><code v-pre>\p{M}</code></a>、<a href="https://util.unicode.org/UnicodeJsps/list-unicodeset.jsp?a=%5B%3AN%3A%5D&amp;g=&amp;i=" target="_blank" rel="noopener noreferrer"><code v-pre>\p{N}</code></a>，以及 ASCII 破折号（<code v-pre>-</code>）和 ASCII 下划线（<code v-pre>_</code>）。</p>
<p>为了将此验证规则限制在 ASCII 范围内的字符（<code v-pre>a-z</code> 和<code v-pre>A-Z</code>），你可以为验证规则提供 <code v-pre>ascii</code> 选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'alpha_dash:ascii'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="alpha-num" tabindex="-1"><a class="header-anchor" href="#alpha-num"><span>alpha_num</span></a></h4>
<p>被验证的字段必须完全是 Unicode 字母数字字符中的 <a href="https://util.unicode.org/UnicodeJsps/list-unicodeset.jsp?a=%5B%3AL%3A%5D&amp;g=&amp;i=" target="_blank" rel="noopener noreferrer"><code v-pre>\p{L}</code></a>, <a href="https://util.unicode.org/UnicodeJsps/list-unicodeset.jsp?a=%5B%3AM%3A%5D&amp;g=&amp;i=" target="_blank" rel="noopener noreferrer"><code v-pre>\p{M}</code></a> 和 <a href="https://util.unicode.org/UnicodeJsps/list-unicodeset.jsp?a=%5B%3AN%3A%5D&amp;g=&amp;i=" target="_blank" rel="noopener noreferrer"><code v-pre>\p{N}</code></a>。</p>
<p>为了将此验证规则限制在 ASCII 范围内的字符（<code v-pre>a-z</code> 和<code v-pre>A-Z</code>），你可以为验证规则提供 <code v-pre>ascii</code> 选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'alpha_num:ascii'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="array" tabindex="-1"><a class="header-anchor" href="#array"><span>array</span></a></h4>
<p>待验证字段必须是有效的 PHP <code v-pre>数组</code>。</p>
<p>当向 <code v-pre>array</code> 规则提供附加值时，输入数组中的每个键都必须出现在提供给规则的值列表中。在以下示例中，输入数组中的 <code v-pre>admin</code> 键无效，因为它不包含在提供给 <code v-pre>array</code> 规则的值列表中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Taylor Otwell'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'taylorotwell'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'admin'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'array:name,username'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>通常，你应该始终指定允许出现在数组中的数组键。</p>
<h4 id="ascii" tabindex="-1"><a class="header-anchor" href="#ascii"><span>ascii</span></a></h4>
<p>正在验证的字段必须完全是 7 位的 ASCII 字符。</p>
<h4 id="bail" tabindex="-1"><a class="header-anchor" href="#bail"><span>bail</span></a></h4>
<p>在首次验证失败后立即终止验证。</p>
<p>虽然 <code v-pre>bail</code> 规则只会在遇到验证失败时停止验证特定字段，但 <code v-pre>stopOnFirstFailure</code> 方法会通知验证器，一旦发生单个验证失败，它应该停止验证所有属性:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">stopOnFirstFailure</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">fails</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="before-date" tabindex="-1"><a class="header-anchor" href="#before-date"><span>before:<em>date</em></span></a></h4>
<p>待验证字段的值对应的日期必须在给定的日期之前。这个日期将被传递给 PHP 函数 <code v-pre>strtotime</code> 以便转化为有效的 <code v-pre>DateTime</code> 实例。此外，与 <a href="#rule-after"><code v-pre>after</code></a> 规则一致，可以将另外一个待验证的字段作为 <code v-pre>date</code> 的值。</p>
<h4 id="before-or-equal-date" tabindex="-1"><a class="header-anchor" href="#before-or-equal-date"><span>before_or_equal:<em>date</em></span></a></h4>
<p>待验证字段的值必须是给定日期之前或等于给定日期的值。这个日期将被传递给 PHP 函数 <code v-pre>strtotime</code> 以便转化为有效的 <code v-pre>DateTime</code> 实例。此外，与 <a href="#rule-after"><code v-pre>after</code></a> 规则一致， 可以将另外一个待验证的字段作为 <code v-pre>date</code> 的值。</p>
<h4 id="between-min-max" tabindex="-1"><a class="header-anchor" href="#between-min-max"><span>between:<em>min</em>,<em>max</em></span></a></h4>
<p>待验证字段值的大小必须介于给定的最小值和最大值（含）之间。字符串、数字、数组和文件的计算方式都使用 <a href="#rule-size"><code v-pre>size</code></a> 方法。</p>
<h4 id="boolean" tabindex="-1"><a class="header-anchor" href="#boolean"><span>boolean</span></a></h4>
<p>验证的字段必须可以转换为 Boolean 类型。 可接受的输入为 <code v-pre>true</code>, <code v-pre>false</code>, <code v-pre>1</code>, <code v-pre>0</code>, <code v-pre>「1」</code>, 和 <code v-pre>「0」</code>。</p>
<h4 id="confirmed" tabindex="-1"><a class="header-anchor" href="#confirmed"><span>confirmed</span></a></h4>
<p>验证字段必须与 <code v-pre>{field}_confirmation</code> 字段匹配。例如，如果验证字段是 <code v-pre>password</code>，则输入中必须存在相应的 <code v-pre>password_confirmation</code> 字段。</p>
<h4 id="current-password" tabindex="-1"><a class="header-anchor" href="#current-password"><span>current_password</span></a></h4>
<p>验证字段必须与已认证用户的密码匹配。 您可以使用规则的第一个参数指定 <a href="https://learnku.com/docs/laravel/10.x/authentication" target="_blank" rel="noopener noreferrer">authentication guard</a>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'current_password:api'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="date" tabindex="-1"><a class="header-anchor" href="#date"><span>date</span></a></h4>
<p>验证字段必须是 <code v-pre>strtotime</code> PHP 函数可识别的有效日期。</p>
<h4 id="date-equals-date" tabindex="-1"><a class="header-anchor" href="#date-equals-date"><span>date_equals:<em>date</em></span></a></h4>
<p>验证字段必须等于给定日期。日期将传递到 PHP <code v-pre>strtotime</code> 函数中，以转换为有效的 <code v-pre>DateTime</code> 实例。</p>
<h4 id="date-format-format" tabindex="-1"><a class="header-anchor" href="#date-format-format"><span>date_format:<em>format</em>,…</span></a></h4>
<p>验证字段必须匹配给定的 <em>format</em> 。在验证字段时，您应该只使用 <code v-pre>date</code> 或 <code v-pre>date_format</code> 中的<strong>其中一个</strong>，而不是同时使用。该验证规则支持 PHP 的 <a href="https://www.php.net/manual/en/class.datetime.php" target="_blank" rel="noopener noreferrer">DateTime</a> 类支持的所有格式。</p>
<h4 id="decimal-min-max" tabindex="-1"><a class="header-anchor" href="#decimal-min-max"><span>decimal:<em>min</em>,<em>max</em></span></a></h4>
<p>验证字段必须是数值类型，并且必须包含指定的小数位数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 必须正好有两位小数（例如 9.99）...</span></span>
<span class="line"><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'decimal:2'</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 必须有 2 到 4 位小数...</span></span>
<span class="line"><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'decimal:2,4'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="declined" tabindex="-1"><a class="header-anchor" href="#declined"><span>declined</span></a></h4>
<p>正在验证的字段必须是 <code v-pre>「no」</code>，<code v-pre>「off」</code>，<code v-pre>0</code> 或者 <code v-pre>false</code>。</p>
<h4 id="declined-if-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#declined-if-anotherfield-value"><span>declined_if:anotherfield,value,…</span></a></h4>
<p>如果另一个验证字段的值等于指定值，则验证字段的值必须为<code v-pre>「no」</code>、<code v-pre>「off」</code>、<code v-pre>0</code>或<code v-pre>false</code>。</p>
<h4 id="different-field" tabindex="-1"><a class="header-anchor" href="#different-field"><span>different:<em>field</em></span></a></h4>
<p>验证的字段值必须与字段 <em>field</em> 的值不同。</p>
<h4 id="digits-value" tabindex="-1"><a class="header-anchor" href="#digits-value"><span>digits:<em>value</em></span></a></h4>
<p>验证的整数必须具有确切长度 <em>value</em> 。</p>
<h4 id="digits-between-min-max" tabindex="-1"><a class="header-anchor" href="#digits-between-min-max"><span>digits_between:<em>min</em>,<em>max</em></span></a></h4>
<p>验证的整数长度必须在给定的 <em>min</em> 和 <em>max</em> 之间。</p>
<h4 id="dimensions" tabindex="-1"><a class="header-anchor" href="#dimensions"><span>dimensions</span></a></h4>
<p>验证的文件必须是符合规则参数指定尺寸限制的图像：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'avatar'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'dimensions:min_width=100,min_height=200'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>可用的限制条件有: <em>min_width</em> , <em>max_width</em> , <em>min_height</em> , <em>max_height</em> , <em>width</em> , <em>height</em> , <em>ratio</em> .</p>
<p><em>ratio</em> 约束应该表示为宽度除以高度。 这可以通过像 <code v-pre>3/2</code> 这样的语句或像 <code v-pre>1.5</code> 这样的浮点数来指定：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'avatar'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'dimensions:ratio=3/2'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>由于此规则需要多个参数，因此你可以 <code v-pre>Rule::dimensions</code> 方法来构造可读性高的规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'avatar'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">dimensions</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">maxWidth</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">maxHeight</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ratio</span><span class="token punctuation">(</span><span class="token number">3</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="distinct" tabindex="-1"><a class="header-anchor" href="#distinct"><span>distinct</span></a></h4>
<p>验证数组时，正在验证的字段不能有任何重复值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'foo.*.id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'distinct'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>默认情况下，Distinct 使用松散的变量比较。要使用严格比较，您可以在验证规则定义中添加 <code v-pre>strict</code> 参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'foo.*.id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'distinct:strict'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以在验证规则的参数中添加 <code v-pre>ignore_case</code> ，以使规则忽略大小写差异：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'foo.*.id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'distinct:ignore_case'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="doesnt-start-with-foo-bar" tabindex="-1"><a class="header-anchor" href="#doesnt-start-with-foo-bar"><span>doesnt_start_with:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>验证的字段不能以给定值之一开头。</p>
<h4 id="doesnt-end-with-foo-bar" tabindex="-1"><a class="header-anchor" href="#doesnt-end-with-foo-bar"><span>doesnt_end_with:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>验证的字段不能以给定值之一结尾。</p>
<h4 id="email" tabindex="-1"><a class="header-anchor" href="#email"><span>email</span></a></h4>
<p>验证的字段必须符合 <code v-pre>e-mail</code> 地址格式。当前版本，此种验证规则由 <a href="https://github.com/egulias/EmailValidator" target="_blank" rel="noopener noreferrer"><code v-pre>egulias/email-validator</code></a> 提供支持。默认情况下，使用 <code v-pre>RFCValidation</code> 验证样式，但你也可以应用其他验证样式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'email:rfc,dns'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>上面的示例将应用 <code v-pre>RFCValidation</code> 和 <code v-pre>DNSCheckValidation</code> 验证。以下是你可以应用的验证样式的完整列表：</p>
<ul>
<li><code v-pre>rfc</code>: <code v-pre>RFCValidation</code></li>
<li><code v-pre>strict</code>: <code v-pre>NoRFCWarningsValidation</code></li>
<li><code v-pre>dns</code>: <code v-pre>DNSCheckValidation</code></li>
<li><code v-pre>spoof</code>: <code v-pre>SpoofCheckValidation</code></li>
<li><code v-pre>filter</code>: <code v-pre>FilterEmailValidation</code></li>
<li><code v-pre>filter_unicode</code>: <code v-pre>FilterEmailValidation::unicode()</code></li>
</ul>
<p><code v-pre>filter</code> 验证器是 Laravel 内置的一个验证器，它使用 PHP 的 <code v-pre>filter_var</code> 函数实现。在 Laravel 5.8 版本之前，它是 Laravel 默认的电子邮件验证行为。</p>
<blockquote>
<p><strong>注意</strong><br>
<code v-pre>dns</code> 和 <code v-pre>spoof</code> 验证器需要 PHP 的 <code v-pre>intl</code> 扩展。</p>
</blockquote>
<h4 id="ends-with-foo-bar" tabindex="-1"><a class="header-anchor" href="#ends-with-foo-bar"><span>ends_with:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>被验证的字段必须以给定值之一结尾。</p>
<h4 id="enum" tabindex="-1"><a class="header-anchor" href="#enum"><span>enum</span></a></h4>
<p><code v-pre>Enum</code> 规则是一种基于类的规则，用于验证被验证字段是否包含有效的枚举值。 <code v-pre>Enum</code> 规则的构造函数只接受枚举的名称作为参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Enums<span class="token punctuation">\</span>ServerStatus</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rules<span class="token punctuation">\</span>Enum</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'status'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token keyword">new</span> <span class="token keyword">Enum</span><span class="token punctuation">(</span><span class="token class-name static-context">ServerStatus</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="exclude" tabindex="-1"><a class="header-anchor" href="#exclude"><span>exclude</span></a></h4>
<p><code v-pre>validate</code> 和 <code v-pre>validated</code> 方法中会排除掉当前验证的字段。</p>
<h4 id="exclude-if-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#exclude-if-anotherfield-value"><span>exclude_if:<em>anotherfield</em>,<em>value</em></span></a></h4>
<p>如果 <em>anotherfield</em> 等于 <em>value</em> ，<code v-pre>validate</code> 和 <code v-pre>validated</code> 方法中会排除掉当前验证的字段。</p>
<p>在一些复杂的场景，也可以使用 <code v-pre>Rule::excludeIf</code> 方法，这个方法需要返回一个布尔值或者一个匿名函数。如果返回的是匿名函数，那么这个函数应该返回 <code v-pre>true</code> 或 <code v-pre>false</code>去决定被验证的字段是否应该被排除掉：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'role_id'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">excludeIf</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">is_admin</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'role_id'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">excludeIf</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">is_admin</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="exclude-unless-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#exclude-unless-anotherfield-value"><span>exclude_unless:<em>anotherfield</em>,<em>value</em></span></a></h4>
<p>除非 <em>anotherfield</em> 等于 <em>value</em> ，否则 <code v-pre>validate</code> 和 <code v-pre>validated</code> 方法中会排除掉当前的字段。如果 <em>value</em> 为 <code v-pre>null</code> （<code v-pre>exclude_unless:name,null</code>），那么成立的条件就是被比较的字段为 <code v-pre>null</code> 或者表单中没有该字段。</p>
<h4 id="exclude-with-anotherfield" tabindex="-1"><a class="header-anchor" href="#exclude-with-anotherfield"><span>exclude_with:<em>anotherfield</em></span></a></h4>
<p>如果表单数据中有 <em>anotherfield</em> ，<code v-pre>validate</code> 和 <code v-pre>validated</code> 方法中会排除掉当前的字段。</p>
<h4 id="exclude-without-anotherfield" tabindex="-1"><a class="header-anchor" href="#exclude-without-anotherfield"><span>exclude_without:<em>anotherfield</em></span></a></h4>
<p>如果表单数据中没有 <em>anotherfield</em> ，<code v-pre>validate</code> 和 <code v-pre>validated</code> 方法中会排除掉当前的字段。</p>
<h4 id="exists-table-column" tabindex="-1"><a class="header-anchor" href="#exists-table-column"><span>exists:<em>table</em>,<em>column</em></span></a></h4>
<p>验证的字段值必须存在于指定的表中。</p>
<h4 id="exists-规则的基本用法" tabindex="-1"><a class="header-anchor" href="#exists-规则的基本用法"><span>Exists 规则的基本用法</span></a></h4>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'state'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'exists:states'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果未指定 <code v-pre>column</code> 选项，则将使用字段名称。因此，在这种情况下，该规则将验证 <code v-pre>states</code> 数据库表是否包含一条记录，该记录的 <code v-pre>state</code> 列的值与请求的 <code v-pre>state</code> 属性值匹配。</p>
<h4 id="指定自定义列名" tabindex="-1"><a class="header-anchor" href="#指定自定义列名"><span>指定自定义列名</span></a></h4>
<p>你可以将验证规则使用的数据库列名称指定在数据库表名称之后：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'state'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'exists:states,abbreviation'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>有时候，你或许需要去明确指定一个具体的数据库连接，用于 <code v-pre>exists</code> 查询。你可以通过在表名前面添加一个连接名称来实现这个效果。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'exists:connection.staff,email'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以明确指定 Eloquent 模型，而不是直接指定表名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'user_id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'exists:App\Models\User,id'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你想要自定义一个执行查询的验证规则，你可以使用 <code v-pre>Rule</code> 类去流畅地定义规则。在这个例子中，我们也将指定验证规则为一个数组，而不再是使用 <code v-pre>|</code> 分割他们：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Query<span class="token punctuation">\</span>Builder</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'staff'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Builder</span> <span class="token variable">$query</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'account_id'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>您可以通过将列名作为 <code v-pre>exists</code> 方法的第二个参数来明确指定 <code v-pre>Rule::exists</code> 方法生成的 <code v-pre>exists</code> 规则应该使用的数据库列名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'state'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'states'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'abbreviation'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="file" tabindex="-1"><a class="header-anchor" href="#file"><span>file</span></a></h4>
<p>要验证的字段必须是一个成功的已经上传的文件。</p>
<h4 id="filled" tabindex="-1"><a class="header-anchor" href="#filled"><span>filled</span></a></h4>
<p>当字段存在时，要验证的字段必须是一个非空的。</p>
<h4 id="gt-field" tabindex="-1"><a class="header-anchor" href="#gt-field"><span>gt:<em>field</em></span></a></h4>
<p>要验证的字段必须要大于给定的字段。这两个字段必须是同一个类型。字符串、数字、数组和文件都使用 <a href="#rule-size"><code v-pre>size</code></a> 进行相同的评估。</p>
<h4 id="gte-field" tabindex="-1"><a class="header-anchor" href="#gte-field"><span>gte:<em>field</em></span></a></h4>
<p>要验证的字段必须要大于或等于给定的字段。这两个字段必须是同一个类型。字符串、数字、数组和文件都使用 <a href="#rule-size"><code v-pre>size</code></a> 进行相同的评估。</p>
<h4 id="image" tabindex="-1"><a class="header-anchor" href="#image"><span>image</span></a></h4>
<p>正在验证的文件必须是图像（jpg、jpeg、png、bmp、gif、svg 或 webp）。</p>
<h4 id="in-foo-bar" tabindex="-1"><a class="header-anchor" href="#in-foo-bar"><span>in:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>验证字段必须包含在给定的值列表中。由于此规则通常要求你 <code v-pre>implode</code> 数组，因此可以使用 <code v-pre>Rule::in</code> 方法来流畅地构造规则:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'zones'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">in</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'first-zone'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'second-zone'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当 <code v-pre>in</code> 规则与 <code v-pre>array</code> 规则组合使用时，输入数组中的每个值都必须出现在提供给 <code v-pre>in</code> 规则的值列表中。 在以下示例中，输入数组中的<code v-pre>LAS</code> 机场代码无效，因为它不包含在提供给 <code v-pre>in</code> 规则的机场列表中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'airports'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'NYC'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'LAS'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'airports'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'array'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'airports.*'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">in</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'NYC'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'LIT'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="in-array-anotherfield" tabindex="-1"><a class="header-anchor" href="#in-array-anotherfield"><span>in_array:<em>anotherfield</em>.*</span></a></h4>
<p>验证的字段必须存在于_anotherfield_的值中。</p>
<h4 id="integer" tabindex="-1"><a class="header-anchor" href="#integer"><span>integer</span></a></h4>
<p>验证的字段必须是一个整数。</p>
<p><strong>警告</strong><br>
这个验证规则并不会验证输入是否为”integer”变量类型，它只会验证输入是否为 PHP 的 <code v-pre>FILTER_VALIDATE_INT</code> 规则接受的类型。如果你需要验证输入是否为数字，请结合 <a href="#rule-numeric">numeric</a> 验证规则使用。</p>
<h4 id="ip" tabindex="-1"><a class="header-anchor" href="#ip"><span>ip</span></a></h4>
<p>验证的字段必须是一个 IP 地址。</p>
<h4 id="ipv4" tabindex="-1"><a class="header-anchor" href="#ipv4"><span>ipv4</span></a></h4>
<p>验证的字段必须是一个 IPv4 地址。</p>
<h4 id="ipv6" tabindex="-1"><a class="header-anchor" href="#ipv6"><span>ipv6</span></a></h4>
<p>验证的字段必须是一个 IPv6 地址。</p>
<h4 id="json" tabindex="-1"><a class="header-anchor" href="#json"><span>json</span></a></h4>
<p>验证的字段必须是一个有效的 JSON 字符串。</p>
<h4 id="lt-field" tabindex="-1"><a class="header-anchor" href="#lt-field"><span>lt:<em>field</em></span></a></h4>
<p>验证的字段必须小于给定的 <em>field</em> 字段。两个字段必须是相同的类型。字符串、数字、数组和文件的处理方式与 <a href="#rule-size"><code v-pre>size</code></a> 规则相同。</p>
<h4 id="lte-field" tabindex="-1"><a class="header-anchor" href="#lte-field"><span>lte:<em>field</em></span></a></h4>
<p>验证的字段必须小于或等于给定的 <em>field</em> 字段。两个字段必须是相同的类型。字符串、数字、数组和文件的处理方式与 <a href="#rule-size"><code v-pre>size</code></a> 规则相同。</p>
<h4 id="lowercase" tabindex="-1"><a class="header-anchor" href="#lowercase"><span>lowercase</span></a></h4>
<p>验证的字段必须是小写的。</p>
<h4 id="mac-address" tabindex="-1"><a class="header-anchor" href="#mac-address"><span>mac_address</span></a></h4>
<p>验证的字段必须是一个 MAC 地址。</p>
<h4 id="max-value" tabindex="-1"><a class="header-anchor" href="#max-value"><span>max:<em>value</em></span></a></h4>
<p>验证的字段的值必须小于或等于最大值 <em>value</em>。字符串、数字、数组和文件的处理方式与 <a href="#rule-size"><code v-pre>size</code></a> 规则相同。</p>
<h4 id="max-digits-value" tabindex="-1"><a class="header-anchor" href="#max-digits-value"><span>max_digits:<em>value</em></span></a></h4>
<p>验证的整数必须具有最大长度 value。</p>
<h4 id="mimetypes-text-plain" tabindex="-1"><a class="header-anchor" href="#mimetypes-text-plain"><span>mimetypes:<em>text/plain</em>,…</span></a></h4>
<p>验证的文件必须匹配给定的 MIME 类型之一：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'video'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'mimetypes:video/avi,video/mpeg,video/quicktime'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>为了确定上传文件的 MIME 类型，将读取文件内容并尝试猜测 MIME 类型，这可能与客户端提供的 MIME 类型不同。</p>
<h4 id="mimes-foo-bar" tabindex="-1"><a class="header-anchor" href="#mimes-foo-bar"><span>mimes:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>验证的文件必须具有与列出的扩展名之一对应的 MIME 类型。</p>
<h4 id="mime-规则的基本用法" tabindex="-1"><a class="header-anchor" href="#mime-规则的基本用法"><span>MIME 规则的基本用法</span></a></h4>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'photo'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'mimes:jpg,bmp,png'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>尽管您只需要指定扩展名，但该规则实际上通过读取文件内容并猜测其 MIME 类型来验证文件的 MIME 类型。可以在以下位置找到 MIME 类型及其相应扩展名的完整列表：</p>
<p><a href="https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types" target="_blank" rel="noopener noreferrer">svn.apache.org/repos/asf/httpd/htt...</a></p>
<h4 id="min-value" tabindex="-1"><a class="header-anchor" href="#min-value"><span>min:<em>value</em></span></a></h4>
<p>验证的字段的值必须大于或等于最小值 <em>value</em>。字符串、数字、数组和文件的处理方式与 <a href="#rule-size"><code v-pre>size</code></a> 规则相同。</p>
<h4 id="min-digits-value" tabindex="-1"><a class="header-anchor" href="#min-digits-value"><span>min_digits:<em>value</em></span></a></h4>
<p>验证的整数必须具有至少_value_位数。</p>
<h4 id="multiple-of-value" tabindex="-1"><a class="header-anchor" href="#multiple-of-value"><span>multiple_of:<em>value</em></span></a></h4>
<p>验证的字段必须是_value_的倍数。</p>
<h4 id="missing" tabindex="-1"><a class="header-anchor" href="#missing"><span>missing</span></a></h4>
<p>验证的字段在输入数据中必须不存在。</p>
<h4 id="missing-if-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#missing-if-anotherfield-value"><span>missing_if:<em>anotherfield</em>,<em>value</em>,…</span></a></h4>
<p>如果_anotherfield_字段等于任何_value_，则验证的字段必须不存在。</p>
<h4 id="missing-unless-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#missing-unless-anotherfield-value"><span>missing_unless:<em>anotherfield</em>,<em>value</em></span></a></h4>
<p>验证的字段必须不存在，除非_anotherfield_字段等于任何_value_。</p>
<h4 id="missing-with-foo-bar" tabindex="-1"><a class="header-anchor" href="#missing-with-foo-bar"><span>missing_with:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>如果任何其他指定的字段存在，则验证的字段必须不存在。</p>
<h4 id="missing-with-all-foo-bar" tabindex="-1"><a class="header-anchor" href="#missing-with-all-foo-bar"><span>missing_with_all:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>如果所有其他指定的字段都存在，则验证的字段必须不存在。</p>
<h4 id="not-in-foo-bar" tabindex="-1"><a class="header-anchor" href="#not-in-foo-bar"><span>not_in:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>验证的字段不能包含在给定值列表中。可以使用<code v-pre>Rule::notIn</code>方法流畅地构建规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'toppings'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">notIn</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'sprinkles'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'cherries'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="not-regex-pattern" tabindex="-1"><a class="header-anchor" href="#not-regex-pattern"><span>not_regex:<em>pattern</em></span></a></h4>
<p>验证的字段必须不匹配给定的正则表达式。</p>
<p>在内部，此规则使用PHP的<code v-pre>preg_match</code>函数。指定的模式应遵守<code v-pre>preg_match</code>所需的相同格式要求，因此也应包括有效的分隔符。例如：<code v-pre>'email' =&gt; 'not_regex:/^.+$/i'</code>。</p>
<p><strong>警告</strong> 使用<code v-pre>regex</code> / <code v-pre>not_regex</code>模式时，可能需要使用数组指定验证规则，而不是使用<code v-pre>|</code>分隔符，特别是如果正则表达式包含<code v-pre>|</code>字符。</p>
<h4 id="nullable" tabindex="-1"><a class="header-anchor" href="#nullable"><span>nullable</span></a></h4>
<p>需要验证的字段可以为 null。</p>
<h4 id="numeric" tabindex="-1"><a class="header-anchor" href="#numeric"><span>numeric</span></a></h4>
<p>需要验证的字段必须是<a href="https://www.php.net/manual/en/function.is-numeric.php" target="_blank" rel="noopener noreferrer">数字类型</a>。</p>
<h4 id="password" tabindex="-1"><a class="header-anchor" href="#password"><span>password</span></a></h4>
<p>需要验证的字段必须与已认证用户的密码相匹配。</p>
<blockquote>
<p><strong>警告</strong><br>
这个规则在 Laravel 9 中被重命名为 <code v-pre>current_password</code> 并计划删除，请改用 <a href="#rule-current-password">Current Password</a> 规则。</p>
</blockquote>
<h4 id="present" tabindex="-1"><a class="header-anchor" href="#present"><span>present</span></a></h4>
<p>需要验证的字段必须存在于输入数据中。</p>
<h4 id="prohibited" tabindex="-1"><a class="header-anchor" href="#prohibited"><span>prohibited</span></a></h4>
<p>需要验证的字段必须不存在或为空。如果符合以下条件之一，字段将被认为是“空”：</p>
<ul>
<li>值为 <code v-pre>null</code>。</li>
<li>值为空字符串。</li>
<li>值为空数组或空的可计数对象。</li>
<li>值为上传文件，但文件路径为空。</li>
</ul>
<h4 id="prohibited-if-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#prohibited-if-anotherfield-value"><span>prohibited_if:<em>anotherfield</em>,<em>value</em>,…</span></a></h4>
<p>如果 anotherfield 字段等于任何 value，则需要验证的字段必须不存在或为空。如果符合以下条件之一，字段将被认为是“空”：</p>
<ul>
<li>值为 <code v-pre>null</code>。</li>
<li>值为空字符串。</li>
<li>值为空数组或空的可计数对象。</li>
<li>值为上传文件，但文件路径为空。</li>
</ul>
<p>如果需要复杂的条件禁止逻辑，则可以使用 <code v-pre>Rule::prohibitedIf</code> 方法。该方法接受一个布尔值或一个闭包。当给定一个闭包时，闭包应返回 <code v-pre>true</code> 或 <code v-pre>false</code>，以指示是否应禁止验证字段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'role_id'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">prohibitedIf</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">is_admin</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'role_id'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">prohibitedIf</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">is_admin</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="prohibited-unless-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#prohibited-unless-anotherfield-value"><span>prohibited_unless:<em>anotherfield</em>,<em>value</em>,…</span></a></h4>
<p>在 anotherfield 字段等于任何 value 时，验证的字段必须为空或缺失。如果一个字段符合以下任一标准，则它被认为是“空”的：</p>
<ul>
<li>值为 <code v-pre>null</code>。</li>
<li>值为空字符串。</li>
<li>值为一个空数组或一个空的 <code v-pre>Countable</code> 对象。</li>
<li>值为上传文件且路径为空。</li>
</ul>
<h4 id="prohibits-anotherfield" tabindex="-1"><a class="header-anchor" href="#prohibits-anotherfield"><span>prohibits:<em>anotherfield</em>,…</span></a></h4>
<p>如果验证的字段不为空或缺失，则 anotherfield 中的所有字段都必须为空或缺失。如果一个字段符合以下任一标准，则它被认为是“空”的：</p>
<ul>
<li>值为 <code v-pre>null</code>。</li>
<li>值为空字符串。</li>
<li>值为一个空数组或一个空的 <code v-pre>Countable</code> 对象。</li>
<li>值为上传文件且路径为空。</li>
</ul>
<h4 id="regex-pattern" tabindex="-1"><a class="header-anchor" href="#regex-pattern"><span>regex:<em>pattern</em></span></a></h4>
<p>验证的字段必须匹配给定的正则表达式。</p>
<p>在内部，此规则使用 PHP 的 <code v-pre>preg_match</code> 函数。指定的模式应遵循 <code v-pre>preg_match</code> 所需的相同格式，并且也包括有效的分隔符。例如：<code v-pre>'email' =&gt; 'regex:/^.+@.+$/i'</code>。</p>
<blockquote>
<p><strong>警告</strong><br>
当使用 <code v-pre>regex</code> / <code v-pre>not_regex</code> 模式时，可能需要使用数组指定规则而不是使用 <code v-pre>|</code> 分隔符，特别是如果正则表达式包含 <code v-pre>|</code> 字符。</p>
</blockquote>
<h4 id="required" tabindex="-1"><a class="header-anchor" href="#required"><span>required</span></a></h4>
<p>验证的字段必须出现在输入数据中且不为空。如果一个字段符合以下任一标准，则它被认为是“空”的：</p>
<ul>
<li>值为 <code v-pre>null</code>。</li>
<li>值为空字符串。</li>
<li>值为一个空数组或一个空的 <code v-pre>Countable</code> 对象。</li>
<li>值为上传文件且路径为空。</li>
</ul>
<h4 id="required-if-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#required-if-anotherfield-value"><span>required_if:<em>anotherfield</em>,<em>value</em>,…</span></a></h4>
<p>如果 anotherfield 字段的值等于任何 value 值，则验证的字段必须存在且不为空。</p>
<p>如果您想要构建更复杂的 <code v-pre>required_if</code> 规则条件，可以使用 <code v-pre>Rule::requiredIf</code> 方法。该方法接受一个布尔值或闭包。当传递一个闭包时，闭包应返回 <code v-pre>true</code> 或 <code v-pre>false</code> 来指示是否需要验证的字段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'role_id'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">requiredIf</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">is_admin</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'role_id'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">requiredIf</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">is_admin</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="required-unless-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#required-unless-anotherfield-value"><span>required_unless:<em>anotherfield</em>,<em>value</em>,…</span></a></h4>
<p>如果 <em>anotherfield</em> 字段的值不等于任何 <em>value</em> 值，则验证的字段必须存在且不为空。这也意味着，除非 <em>anotherfield</em> 字段等于任何 <em>value</em> 值，否则必须在请求数据中包含 <em>anotherfield</em> 字段。如果 <em>value</em> 的值为 <code v-pre>null</code> （<code v-pre>required_unless:name,null</code>），则必须验证该字段，除非比较字段是 <code v-pre>null</code> 或比较字段不存在于请求数据中。</p>
<h4 id="required-with-foo-bar" tabindex="-1"><a class="header-anchor" href="#required-with-foo-bar"><span>required_with:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>仅当任何其他指定字段存在且不为空时，才需要验证字段存在且不为空。</p>
<h4 id="required-with-all-foo-bar" tabindex="-1"><a class="header-anchor" href="#required-with-all-foo-bar"><span>required_with_all:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>仅当所有其他指定字段存在且不为空时，才需要验证字段存在且不为空。</p>
<h4 id="required-without-foo-bar" tabindex="-1"><a class="header-anchor" href="#required-without-foo-bar"><span>required_without:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>验证的字段仅在任一其他指定字段为空或不存在时，必须存在且不为空。</p>
<h4 id="required-without-all-foo-bar" tabindex="-1"><a class="header-anchor" href="#required-without-all-foo-bar"><span>required_without_all:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>验证的字段仅在所有其他指定字段为空或不存在时，必须存在且不为空。</p>
<h4 id="required-array-keys-foo-bar" tabindex="-1"><a class="header-anchor" href="#required-array-keys-foo-bar"><span>required_array_keys:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>验证的字段必须是一个数组，并且必须至少包含指定的键。</p>
<h4 id="same-field" tabindex="-1"><a class="header-anchor" href="#same-field"><span>same:<em>field</em></span></a></h4>
<p>给定的字段必须与验证的字段匹配。</p>
<h4 id="size-value" tabindex="-1"><a class="header-anchor" href="#size-value"><span>size:<em>value</em></span></a></h4>
<p>验证的字段必须具有与给定的_value相匹配的大小。对于字符串数据，value 对应于字符数。对于数字数据，value 对应于给定的整数值（该属性还必须具有 numeric 或 integer 规则）。对于数组，size 对应于数组的 count。对于文件，size 对应于文件大小（以千字节为单位）。让我们看一些例子：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// Validate that a string is exactly 12 characters long...</span></span>
<span class="line"><span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'size:12'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Validate that a provided integer equals 10...</span></span>
<span class="line"><span class="token string single-quoted-string">'seats'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'integer|size:10'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Validate that an array has exactly 5 elements...</span></span>
<span class="line"><span class="token string single-quoted-string">'tags'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'array|size:5'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Validate that an uploaded file is exactly 512 kilobytes...</span></span>
<span class="line"><span class="token string single-quoted-string">'image'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'file|size:512'</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="starts-with-foo-bar" tabindex="-1"><a class="header-anchor" href="#starts-with-foo-bar"><span>starts_with:<em>foo</em>,<em>bar</em>,…</span></a></h4>
<p>验证的字段必须以给定值之一开头。</p>
<h4 id="string" tabindex="-1"><a class="header-anchor" href="#string"><span>string</span></a></h4>
<p>验证的字段必须是一个字符串。如果您希望允许字段也可以为 <code v-pre>null</code>，则应将 <code v-pre>nullable</code> 规则分配给该字段。</p>
<h4 id="时区" tabindex="-1"><a class="header-anchor" href="#时区"><span>时区</span></a></h4>
<p>验证字段必须是一个有效的时区标识符，符合 <code v-pre>timezone_identifiers_list</code> PHP 函数的要求。</p>
<h4 id="unique-table-column" tabindex="-1"><a class="header-anchor" href="#unique-table-column"><span>unique:<em>table</em>,<em>column</em></span></a></h4>
<p>验证字段在给定的数据库表中必须不存在。</p>
<p><strong>指定自定义表/列名:</strong></p>
<p>可以指定应使用哪个 Eloquent 模型来确定表名，而不是直接指定表名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'unique:App\Models\User,email_address'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>column</code> 选项可用于指定字段对应的数据库列。如果未指定 <code v-pre>column</code> 选项，则使用验证字段的名称。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'unique:users,email_address'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><strong>指定自定义数据库连接</strong></p>
<p>有时，您可能需要为 Validator 执行的数据库查询设置自定义连接。为此，可以在表名之前添加连接名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'unique:connection.users,email_address'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><strong>强制唯一规则忽略给定的 ID:</strong></p>
<p>有时，您可能希望在唯一验证期间忽略给定的 ID。例如，考虑一个“更新个人资料”屏幕，其中包括用户的姓名、电子邮件地址和位置。您可能希望验证电子邮件地址是否唯一。但是，如果用户仅更改了名称字段而未更改电子邮件字段，则不希望因为用户已经拥有相关电子邮件地址而抛出验证错误。</p>
<p>要指示验证器忽略用户的 ID，我们将使用 <code v-pre>Rule</code> 类来流畅地定义规则。在此示例中，我们还将指定验证规则作为数组，而不是使用 <code v-pre>|</code> 字符来分隔规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Builder</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ignore</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>警告</strong><br>
您不应将任何用户控制的请求输入传递到 <code v-pre>ignore</code> 方法中。相反，您应仅传递系统生成的唯一 ID，例如 Eloquent 模型实例的自增 ID 或 UUID。否则，您的应用程序将容易受到 SQL 注入攻击。</p>
</blockquote>
<p>不需要将模型键的值传递给 <code v-pre>ignore</code> 方法，您也可以传递整个模型实例。Laravel 将自动从模型中提取键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ignore</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果您的表使用的是除 <code v-pre>id</code> 以外的主键列名，可以在调用 <code v-pre>ignore</code> 方法时指定列的名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ignore</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>默认情况下，<code v-pre>unique</code> 规则将检查与正在验证的属性名称匹配的列的唯一性。但是，您可以将不同的列名称作为第二个参数传递给 <code v-pre>unique</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email_address'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ignore</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><strong>添加额外的查询条件：</strong></p>
<p>您可以通过自定义查询并使用 <code v-pre>where</code> 方法来指定其他查询条件。例如，让我们添加一个查询条件，将查询范围限定为仅搜索具有 <code v-pre>account_id</code> 列值为 <code v-pre>1</code> 的记录：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Builder</span> <span class="token variable">$query</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'account_id'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="uppercase" tabindex="-1"><a class="header-anchor" href="#uppercase"><span>uppercase</span></a></h4>
<p>验证字段必须为大写。</p>
<h4 id="url" tabindex="-1"><a class="header-anchor" href="#url"><span>url</span></a></h4>
<p>验证字段必须为有效的 URL。</p>
<h4 id="ulid" tabindex="-1"><a class="header-anchor" href="#ulid"><span>ulid</span></a></h4>
<p>验证字段必须为有效的<a href="https://github.com/ulid/spec" target="_blank" rel="noopener noreferrer">通用唯一词典排序标识符</a>（ULID）。</p>
<h4 id="uuid" tabindex="-1"><a class="header-anchor" href="#uuid"><span>uuid</span></a></h4>
<p>验证字段必须为有效的 RFC 4122（版本1、3、4或5）通用唯一标识符（UUID）。</p>
<h2 id="有条件添加规则" tabindex="-1"><a class="header-anchor" href="#有条件添加规则"><span>有条件添加规则</span></a></h2>
<h4 id="当字段具有特定值时跳过验证" tabindex="-1"><a class="header-anchor" href="#当字段具有特定值时跳过验证"><span>当字段具有特定值时跳过验证</span></a></h4>
<p>有时，您可能希望在给定字段具有特定值时不验证另一个字段。您可以使用<code v-pre>exclude_if</code>验证规则来实现这一点。在下面的示例中，如果<code v-pre>has_appointment</code>字段的值为<code v-pre>false</code>，则不会验证<code v-pre>appointment_date</code>和<code v-pre>doctor_name</code>字段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'has_appointment'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|boolean'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'appointment_date'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'exclude_if:has_appointment,false|required|date'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'doctor_name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'exclude_if:has_appointment,false|required|string'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，您可以使用<code v-pre>exclude_unless</code>规则，除非另一个字段具有给定值，否则不验证给定字段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'has_appointment'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|boolean'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'appointment_date'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'exclude_unless:has_appointment,true|required|date'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'doctor_name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'exclude_unless:has_appointment,true|required|string'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="仅在字段存在时验证" tabindex="-1"><a class="header-anchor" href="#仅在字段存在时验证"><span>仅在字段存在时验证</span></a></h4>
<p>在某些情况下，您可能希望仅在验证数据中存在该字段时才对该字段运行验证检查。要快速实现此操作，请将<code v-pre>sometimes</code>规则添加到您的规则列表中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$v</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'sometimes|required|email'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在上面的示例中，如果<code v-pre>$data</code>数组中存在<code v-pre>email</code>字段，则仅对其进行验证。</p>
<blockquote>
<p><strong>注意</strong><br>
如果您尝试验证始终应存在但可能为空的字段，请查看<a href="#a-note-on-optional-fields">有关可选字段的说明</a>。</p>
</blockquote>
<h4 id="复杂条件验证" tabindex="-1"><a class="header-anchor" href="#复杂条件验证"><span>复杂条件验证</span></a></h4>
<p>有时，您可能希望根据更复杂的条件逻辑添加验证规则。例如，您可能只希望在另一个字段的值大于100时要求给定字段。或者，只有在存在另一个字段时，两个字段才需要具有给定值。添加这些验证规则不必是痛苦的。首先，使用永不改变的静态规则创建一个<code v-pre>Validator</code>实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|email'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'games'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|numeric'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>假设我们的 Web 应用是给游戏收藏家使用的。如果一个游戏收藏家在我们的应用上注册，并且他们拥有超过 100 个游戏，我们想要让他们解释为什么拥有这么多游戏。例如，也许他们经营着一家游戏转售店，或者他们只是喜欢收集游戏。为了有条件地添加这个要求，我们可以在 <code v-pre>Validator</code> 实例上使用 <code v-pre>sometimes</code> 方法。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Fluent</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">sometimes</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'reason'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'required|max:500'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Fluent</span> <span class="token variable">$input</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$input</span><span class="token operator">-></span><span class="token property">games</span> <span class="token operator">>=</span> <span class="token number">100</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>传递给 <code v-pre>sometimes</code> 方法的第一个参数是我们有条件验证的字段的名称。第二个参数是我们想要添加的规则列表。如果传递作为第三个参数的闭包返回 <code v-pre>true</code>，这些规则将被添加。使用此方法可以轻松构建复杂的条件验证。您甚至可以同时为多个字段添加条件验证：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">sometimes</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'reason'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'cost'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Fluent</span> <span class="token variable">$input</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$input</span><span class="token operator">-></span><span class="token property">games</span> <span class="token operator">>=</span> <span class="token number">100</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
传递给您的闭包的 <code v-pre>$input</code> 参数将是 <code v-pre>Illuminate\Support\Fluent</code> 的一个实例，可用于访问您正在验证的输入和文件。</p>
</blockquote>
<h4 id="复杂条件数组验证" tabindex="-1"><a class="header-anchor" href="#复杂条件数组验证"><span>复杂条件数组验证</span></a></h4>
<p>有时，您可能想要基于同一嵌套数组中的另一个字段验证一个字段，而您不知道其索引。在这种情况下，您可以允许您的闭包接收第二个参数，该参数将是正在验证的当前个体数组项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'channels'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'type'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'address'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'abigail@example.com'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'type'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'url'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'address'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'https://example.com'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">sometimes</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'channels.*.address'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Fluent</span> <span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Fluent</span> <span class="token variable">$item</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$item</span><span class="token operator">-></span><span class="token property">type</span> <span class="token operator">===</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">sometimes</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'channels.*.address'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'url'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Fluent</span> <span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Fluent</span> <span class="token variable">$item</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$item</span><span class="token operator">-></span><span class="token property">type</span> <span class="token operator">!==</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>像传递给闭包的 <code v-pre>$input</code> 参数一样，当属性数据是数组时，<code v-pre>$item</code> 参数是 <code v-pre>Illuminate\Support\Fluent</code> 的实例；否则，它是一个字符串。</p>
<h2 id="验证数组" tabindex="-1"><a class="header-anchor" href="#验证数组"><span>验证数组</span></a></h2>
<p>正如在 <a href="#rule-array"><code v-pre>array</code> 验证规则文档</a> 中讨论的那样，<code v-pre>array</code> 规则接受允许的数组键列表。如果数组中存在任何额外的键，则验证将失败：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Taylor Otwell'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'taylorotwell'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'admin'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'array:username,locale'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>通常情况下，您应该始终指定允许出现在数组中的键。否则，验证器的 <code v-pre>validate</code> 和 <code v-pre>validated</code> 方法将返回所有经过验证的数据，包括数组及其所有键，即使这些键没有通过其他嵌套数组验证规则进行验证。</p>
<h3 id="验证嵌套数组输入" tabindex="-1"><a class="header-anchor" href="#验证嵌套数组输入"><span>验证嵌套数组输入</span></a></h3>
<p>验证基于嵌套数组的表单输入字段并不需要很痛苦。您可以使用 “点符号” 来验证数组中的属性。例如，如果传入的 HTTP 请求包含一个 <code v-pre>photos[profile]</code> 字段，您可以像这样验证它：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'photos.profile'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|image'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>您还可以验证数组中的每个元素。例如，要验证给定数组输入字段中的每个电子邮件是否唯一，可以执行以下操作：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'person.*.email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'email|unique:users'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'person.*.first_name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required_with:person.*.last_name'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>同样，您可以在语言文件中指定<a href="#custom-messages-for-specific-attributes">自定义验证消息</a>时使用 <code v-pre>*</code> 字符，使得针对基于数组的字段使用单个验证消息变得非常简单：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'custom'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'person.*.email'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'unique'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Each person must have a unique email address'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="访问嵌套数组数据" tabindex="-1"><a class="header-anchor" href="#访问嵌套数组数据"><span>访问嵌套数组数据</span></a></h4>
<p>有时，当为属性分配验证规则时，您可能需要访问给定嵌套数组元素的值。您可以使用 <code v-pre>Rule::forEach</code> 方法来实现此目的。<code v-pre>forEach</code> 方法接受一个闭包，在验证数组属性的每次迭代中调用该闭包，并接收属性的值和显式的完全展开的属性名称。闭包应该返回要分配给数组元素的规则数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Rules<span class="token punctuation">\</span>HasPermission</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'companies.*.id'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-declaration">string</span><span class="token operator">|</span><span class="token keyword type-declaration">null</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$attribute</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token class-name static-context">Company</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token keyword">new</span> <span class="token class-name">HasPermission</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'manage-company'</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="错误消息索引和位置" tabindex="-1"><a class="header-anchor" href="#错误消息索引和位置"><span>错误消息索引和位置</span></a></h3>
<p>在验证数组时，您可能希望在应用程序显示的错误消息中引用失败验证的特定项的索引或位置。为了实现这一点，您可以在<a href="#manual-customizing-the-error-messages">自定义验证消息</a>中包含 <code v-pre>:index</code>（从 <code v-pre>0</code> 开始）和 <code v-pre>:position</code>（从 <code v-pre>1</code> 开始）占位符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'photos'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'BeachVacation.jpg'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'description'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'我的海滩假期照片！'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'GrandCanyon.jpg'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'description'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">''</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'photos.*.description'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'photos.*.description.required'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'请描述第 :position 张照片。'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>上述示例将验证失败，并且用户会看到以下错误：“请描述第 2 张照片。”</p>
<h2 id="验证文件" tabindex="-1"><a class="header-anchor" href="#验证文件"><span>验证文件</span></a></h2>
<p>Laravel提供了多种上传文件的验证规则，如<code v-pre>mimes</code>、<code v-pre>image</code>、<code v-pre>min</code>和<code v-pre>max</code>。虽然你可以在验证文件时单独指定这些规则，但Laravel还是提供了一个流畅的文件验证规则生成器，你可能会觉得更方便：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rules<span class="token punctuation">\</span>File</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'attachment'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token class-name static-context">File</span><span class="token operator">::</span><span class="token function">types</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'mp3'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'wav'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">1024</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">max</span><span class="token punctuation">(</span><span class="token number">12</span> <span class="token operator">*</span> <span class="token number">1024</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你的程序允许用户上传图片，那么可以使用<code v-pre>File</code> 规则的 <code v-pre>image</code> 构造方法来指定上传的文件应该是图片。另外， <code v-pre>dimensions</code> 规则可用于限制图片的尺寸：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rules<span class="token punctuation">\</span>File</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'photo'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token class-name static-context">File</span><span class="token operator">::</span><span class="token function">image</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">1024</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">max</span><span class="token punctuation">(</span><span class="token number">12</span> <span class="token operator">*</span> <span class="token number">1024</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">dimensions</span><span class="token punctuation">(</span><span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">dimensions</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">maxWidth</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">maxHeight</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>技巧</strong><br>
更多验证图片尺寸的信息，请参见<a href="#rule-dimensions">尺寸规则文档</a>。</p>
</blockquote>
<h4 id="文件类型" tabindex="-1"><a class="header-anchor" href="#文件类型"><span>文件类型</span></a></h4>
<p>尽管在调用 <code v-pre>types</code> 方法时只需要指定扩展名，但该方法实际上是通过读取文件的内容并猜测其MIME类型来验证文件的MIME类型的。MIME类型及其相应扩展的完整列表可以在以下链接中找到：</p>
<p><a href="https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types" target="_blank" rel="noopener noreferrer">svn.apache.org/repos/asf/httpd/htt...</a></p>
<h2 id="验证密码" tabindex="-1"><a class="header-anchor" href="#验证密码"><span>验证密码</span></a></h2>
<p>为确保密码具有足够的复杂性，你可以使用 Laravel 的 <code v-pre>password</code> 规则对象：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rules<span class="token punctuation">\</span>Password</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'confirmed'</span><span class="token punctuation">,</span> <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>Password</code> 规则对象允许你轻松自定义应用程序的密码复杂性要求，例如指定密码至少需要一个字母、数字、符号或混合大小写的字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token comment">// 至少需要 8 个字符...</span></span>
<span class="line">    <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 至少需要一个字母...</span></span>
<span class="line">    <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">letters</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 至少需要一个大写字母和一个小写字母...</span></span>
<span class="line">    <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">mixedCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 至少需要一个数字...</span></span>
<span class="line">    <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">numbers</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 至少需要一个符号...</span></span>
<span class="line">    <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">symbols</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此外，你可以使用 <code v-pre>uncompromised</code> 方法确保密码没有在公共密码数据泄露事件中被泄露：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">uncompromised</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>在内部，<code v-pre>Password</code> 规则对象使用 <a href="https://en.wikipedia.org/wiki/K-anonymity" target="_blank" rel="noopener noreferrer">k-Anonymity</a> 模型来确定密码是否已通过 <a href="https://haveibeenpwned.com/" target="_blank" rel="noopener noreferrer">haveibeenpwned.com</a> 服务而不牺牲用户的隐私或安全。</p>
<p>默认情况下，如果密码在数据泄露中至少出现一次，则会被视为已泄露。你可以使用 <code v-pre>uncompromised</code> 方法的第一个参数自定义此阈值</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token comment">// Ensure the password appears less than 3 times in the same data leak...</span></span>
<span class="line">    <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">uncompromised</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>当然，你可以将上面示例中的所有方法链接起来：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">letters</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">mixedCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">numbers</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">symbols</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">uncompromised</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="定义默认密码规则" tabindex="-1"><a class="header-anchor" href="#定义默认密码规则"><span>定义默认密码规则</span></a></h4>
<p>你可能会发现在应用程序的单个位置指定密码的默认验证规则很方便。你可以使用接受闭包的 <code v-pre>Password::defaults</code> 方法轻松完成此操作。给 <code v-pre>defaults</code> 方法的闭包应该返回密码规则的默认配置。通常，应该在应用程序服务提供者之一的 <code v-pre>boot</code> 方法中调用 <code v-pre>defaults</code> 规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rules<span class="token punctuation">\</span>Password</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导任何应用程序服务</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">defaults</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$rule</span> <span class="token operator">=</span> <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">app</span><span class="token operator">-></span><span class="token function">isProduction</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">?</span> <span class="token variable">$rule</span><span class="token operator">-></span><span class="token function">mixedCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">uncompromised</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token punctuation">:</span> <span class="token variable">$rule</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>然后，当你想将默认规则应用于正在验证的特定密码时，你可以调用不带参数的 <code v-pre>defaults</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span> <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">defaults</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>有时，你可能希望将其他验证规则附加到默认密码验证规则。 你可以使用 <code v-pre>rules</code> 方法来完成此操作：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Rules<span class="token punctuation">\</span>ZxcvbnRule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">defaults</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$rule</span> <span class="token operator">=</span> <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">rules</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token keyword">new</span> <span class="token class-name">ZxcvbnRule</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="自定义验证规则" tabindex="-1"><a class="header-anchor" href="#自定义验证规则"><span>自定义验证规则</span></a></h2>
<h3 id="使用规则对象" tabindex="-1"><a class="header-anchor" href="#使用规则对象"><span>使用规则对象</span></a></h3>
<p>Laravel 提供了各种有用的验证规则；但是，你可能希望指定一些你自己的。 注册自定义验证规则的一种方法是使用规则对象。 要生成新的规则对象，你可以使用 <code v-pre>make:rule</code> Artisan 命令。 让我们使用这个命令生成一个规则来验证字符串是否为大写。 Laravel 会将新规则放在 <code v-pre>app/Rules</code> 目录中。 如果这个目录不存在，Laravel 会在你执行 Artisan 命令创建规则时创建它：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan make:rule Uppercase</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>一旦规则被创建，我们就可以定义其行为。一个规则对象包含一个单一的方法：<code v-pre>validate</code>。该方法接收属性名、其值和一个回调函数，如果验证失败应该调用该回调函数并传入验证错误消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Rules</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Closure</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>ValidationRule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Uppercase</span> <span class="token keyword">implements</span> <span class="token class-name">ValidationRule</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * Run the validation rule.</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">validate</span><span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$attribute</span><span class="token punctuation">,</span> <span class="token keyword type-hint">mixed</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Closure</span> <span class="token variable">$fail</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">strtoupper</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">!==</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$fail</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'The :attribute must be uppercase.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦定义了规则，您可以通过将规则对象的实例与其他验证规则一起传递来将其附加到验证器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Rules<span class="token punctuation">\</span>Uppercase</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'string'</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">Uppercase</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="验证消息" tabindex="-1"><a class="header-anchor" href="#验证消息"><span>验证消息</span></a></h4>
<p>您可以不提供 <code v-pre>$fail</code> 闭包的字面错误消息，而是提供一个<a href="https://chat.openai.com/docs/laravel/10.x/localization" target="_blank" rel="noopener noreferrer">翻译字符串键</a>，并指示 Laravel 翻译错误消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">strtoupper</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">!==</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$fail</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'validation.uppercase'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">translate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如有必要，您可以通过第一个和第二个参数分别提供占位符替换和首选语言来调用 <code v-pre>translate</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$fail</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'validation.location'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">translate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'value'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">value</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'fr'</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="访问额外数据" tabindex="-1"><a class="header-anchor" href="#访问额外数据"><span>访问额外数据</span></a></h4>
<p>如果您的自定义验证规则类需要访问正在验证的所有其他数据，则规则类可以实现 <code v-pre>Illuminate\Contracts\Validation\DataAwareRule</code> 接口。此接口要求您的类定义一个 <code v-pre>setData</code> 方法。Laravel 会自动调用此方法（在验证继续之前）并传入所有正在验证的数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Rules</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>DataAwareRule</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>ValidationRule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Uppercase</span> <span class="token keyword">implements</span> <span class="token class-name">DataAwareRule</span><span class="token punctuation">,</span> ValidationRule</span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 正在验证的所有数据。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, mixed></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 设置正在验证的数据。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, mixed>  $data</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">setData</span><span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">static</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">data</span> <span class="token operator">=</span> <span class="token variable">$data</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，如果您的验证规则需要访问执行验证的验证器实例，则可以实现<code v-pre>ValidatorAwareRule</code>接口：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Rules</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>ValidationRule</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>ValidatorAwareRule</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Uppercase</span> <span class="token keyword">implements</span> <span class="token class-name">ValidationRule</span><span class="token punctuation">,</span> ValidatorAwareRule</span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 验证器实例.</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name"><span class="token punctuation">\</span>Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Validator</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 设置当前验证器.</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">setValidator</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Validator</span> <span class="token variable">$validator</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">static</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">validator</span> <span class="token operator">=</span> <span class="token variable">$validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="使用闭包函数" tabindex="-1"><a class="header-anchor" href="#使用闭包函数"><span>使用闭包函数</span></a></h3>
<p>如果您只需要在应用程序中一次使用自定义规则的功能，可以使用闭包函数而不是规则对象。闭包函数接收属性名称、属性值和 $fail 回调函数，如果验证失败，应该调用该函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'max:255'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$attribute</span><span class="token punctuation">,</span> <span class="token keyword type-hint">mixed</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Closure</span> <span class="token variable">$fail</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$value</span> <span class="token operator">===</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token variable">$fail</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"The <span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$attribute</span><span class="token punctuation">}</span></span> is invalid."</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="隐式规则" tabindex="-1"><a class="header-anchor" href="#隐式规则"><span>隐式规则</span></a></h3>
<p>默认情况下，当要验证的属性不存在或包含空字符串时，正常的验证规则，包括自定义规则，都不会执行。例如，<a href="#rule-unique"><code v-pre>unique</code></a> 规则不会针对空字符串运行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$rules</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'unique:users,name'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">''</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token variable">$rules</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">passes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>为了使自定义规则在属性为空时也运行，规则必须暗示该属性是必需的。您可以使用 make:rule Artisan 命令的 –implicit 选项快速生成新的隐式规则对象：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan make:rule Uppercase <span class="token parameter variable">--implicit</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>*<em>警告 *</em><br>
隐式规则仅 暗示 该属性是必需的。实际上，缺少或空属性是否无效取决于您。</p>
</blockquote>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/10.x/validation/14856" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/va...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/10.x/validation/14856" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/va...</a></p>
</blockquote>
</div></template>


